#include "GGE.h"

GGE_Text::GGE_Text ()
{
  varGGE_TEXT_COLOR.r = 255;
  varGGE_TEXT_COLOR.g = 255;
  varGGE_TEXT_COLOR.b = 255;
}

void GGE_Text::Load ( const char * urlPath , int fontSize )
{
  varGGE_TEXT_FONT = TTF_OpenFont ( urlPath, fontSize );
  if ( varGGE_TEXT_FONT == 0 )
    {
      exit(0);
    }

}

void GGE_Text::Color( float r, float g, float b )
{
  varGGE_TEXT_COLOR.r = r;
  varGGE_TEXT_COLOR.g = g;
  varGGE_TEXT_COLOR.b = b;
}


void GGE_Text::Write(const char *text, int x__, int y__ )
{
  SDL_Surface *textSurface;
  textSurface=TTF_RenderText_Blended(varGGE_TEXT_FONT,text,varGGE_TEXT_COLOR);
  GLuint varGGE_TEXTURE;

  glGenTextures( 1, &varGGE_TEXTURE );
  glBindTexture( GL_TEXTURE_2D, varGGE_TEXTURE );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  glTexImage2D( GL_TEXTURE_2D, 0, 4, textSurface->w, textSurface->h, 0,
                GL_BGRA, GL_UNSIGNED_BYTE, textSurface->pixels );
  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  glBindTexture( GL_TEXTURE_2D,varGGE_TEXTURE );


  glTranslatef (x__   , y__   ,0 );
  glLoadIdentity();
  glPushMatrix();

  glBegin( GL_QUADS );
  glTexCoord2i( 0, 0 );
  glVertex3f( x__, y__, 0 );
  glTexCoord2i( 1, 0 );
  glVertex3f( x__ + textSurface->w, y__, 0 );
  glTexCoord2i( 1, 1 );
  glVertex3f( x__ + textSurface->w, y__ + textSurface->h, 0 );
  glTexCoord2i( 0, 1 );
  glVertex3f( x__, y__+ textSurface->h, 0 );
  glEnd();
  glFinish();

  SDL_FreeSurface ( textSurface);
  glDeleteTextures(1, &varGGE_TEXTURE);
}

void GGE_Text::Write(float val, int x, int y )
{
  SDL_Surface *textSurface;

  char textF[255];
  sprintf( textF, "%f", val );

  textSurface=TTF_RenderText_Blended(varGGE_TEXT_FONT,textF,varGGE_TEXT_COLOR);
  GLuint varGGE_TEXTURE;

  glGenTextures( 1, &varGGE_TEXTURE );
  glBindTexture( GL_TEXTURE_2D, varGGE_TEXTURE );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  glTexImage2D( GL_TEXTURE_2D, 0, 4, textSurface->w, textSurface->h, 0,
                GL_BGRA, GL_UNSIGNED_BYTE, textSurface->pixels );
  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  glBindTexture( GL_TEXTURE_2D,varGGE_TEXTURE );
  glTranslatef (x    , y    ,0 );
  glLoadIdentity();
  glPushMatrix();

  glBegin( GL_QUADS );
  glTexCoord2i( 0, 0 );
  glVertex3f( x, y, 0 );
  glTexCoord2i( 1, 0 );
  glVertex3f( x + textSurface->w, y, 0 );
  glTexCoord2i( 1, 1 );
  glVertex3f( x + textSurface->w, y + textSurface->h, 0 );
  glTexCoord2i( 0, 1 );
  glVertex3f( x, y+ textSurface->h, 0 );
  glEnd();
  glFinish();

  SDL_FreeSurface ( textSurface);
  glDeleteTextures(1, &varGGE_TEXTURE);
}
void GGE_Text::Write(int val, int x, int y )
{
  SDL_Surface *textSurface;

  char textF[255];
  sprintf( textF, "%i", val );

  textSurface=TTF_RenderText_Blended(varGGE_TEXT_FONT,textF,varGGE_TEXT_COLOR);
  GLuint varGGE_TEXTURE;

  glGenTextures( 1, &varGGE_TEXTURE );
  glBindTexture( GL_TEXTURE_2D, varGGE_TEXTURE );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
  glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
  glTexImage2D( GL_TEXTURE_2D, 0, 4, textSurface->w, textSurface->h, 0,
                GL_BGRA, GL_UNSIGNED_BYTE, textSurface->pixels );
  glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
  glBindTexture( GL_TEXTURE_2D,varGGE_TEXTURE );
  glTranslatef (x    , y    ,0 );
  glLoadIdentity();
  glPushMatrix();

  glBegin( GL_QUADS );
  glTexCoord2i( 0, 0 );
  glVertex3f( x, y, 0 );
  glTexCoord2i( 1, 0 );
  glVertex3f( x + textSurface->w, y, 0 );
  glTexCoord2i( 1, 1 );
  glVertex3f( x + textSurface->w, y + textSurface->h, 0 );
  glTexCoord2i( 0, 1 );
  glVertex3f( x, y+ textSurface->h, 0 );
  glEnd();
  glFinish();

  SDL_FreeSurface ( textSurface);
  glDeleteTextures(1, &varGGE_TEXTURE);
}
